package com.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import com.entity.Admins;
import com.entity.User;


@Repository
public class adminDaoImpl {
	@Resource
	private SessionFactory sessionFactory;
	
	//查询管理员
	public List<Admins> findAll(){
		String hql="from Admins";
		Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
		List<Admins> list=query.list();
		return list;
	}
	
	//查询用户
	public List<User> findAllUser(){
		String hql="from User";
		Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
		List<User> list=query.list();
		return list;
	}
	
	//登录
	public boolean adminByName(String name){
		Admins a=null;
		String hql="from Admins a where a.name=?";
		try{
			Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
			query.setParameter(0, name);
			a=(Admins)query.uniqueResult();
		}catch(Exception e){
			e.printStackTrace();
		}
		if(a!=null){
			return true;
		}else {
			return false;
		}
	}
	
	public boolean adminByNameAndPW(String name,String password){
		Admins a = null;
		String hql="from Admins a where a.name=? and a.password=?";
		try{
			Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
			query.setParameter(0,name);
			query.setParameter(1,password);
			a=(Admins)query.uniqueResult();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (a != null) {
			return true;
		} else {
			return false;
		}
	}
	
	//id查询管理员
	public Admins findAdminById(int id){
		String hql="from Admins where id=?";
		Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
		Admins admin=(Admins)query.uniqueResult();
		return admin;
		
	}
	//修改管理员信息
	public void updateadmin(Admins a){
		this.sessionFactory.getCurrentSession().update(a);
	}
	
	//id查询用户
	public User findUserById(int id){
		String hql="from User where id=?";
		Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, id);
		User user=(User)query.uniqueResult();
		return user;
	}
	
	//修改用户信息
	public void updateUser(User u){
		this.sessionFactory.getCurrentSession().update(u);
	}
	//删除用户
	public void deletUser(User u){
		this.sessionFactory.getCurrentSession().delete(u);
	}

	//忘记密码
	
}
